package org.ljy.springblog.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.ljy.springblog.model.BlogInfo;
import org.ljy.springblog.model.UserInfo;
import org.ljy.springblog.service.BlogService;
import org.ljy.springblog.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     *  展示所有博客列表
     */
    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        return blogService.getBlogList();
    }
    /**
     *  展示博客详情页
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        log.info("blogId: " + blogId);
        Assert.notNull(blogId,"blogId is null");
        BlogInfo blogInfo = blogService.getBlogById(blogId);
        //1.获取当前文章的作者id
        Integer authorId = blogInfo.getUserId();
        //2.获取当前登陆用户id
        String token = request.getHeader("user_token_header");
        Integer userId = JwtUtils.getUserIdByToken(token);
        //3.判断
        if(authorId != null && authorId == userId) {
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }

    /**
     * 发布博客
     */
    @RequestMapping("/add")
    public boolean addBlog(@Validated @RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("发布博客~");
        //1.获取登陆的用户信息
        String token = request.getHeader("user_token_header");
        Integer userId = JwtUtils.getUserIdByToken(token);
        //2.设置插入id
        blogInfo.setUserId(userId);
        //3.插入（可能会发送异常）
        Integer result = null;
        try {
            result = blogService.addBlog(blogInfo);
            if(result==1) return true;
        }catch (Exception e) {
            log.error("发布博客失败！");
        }
        return false;
    }
    /**
     * 更新博客
     */
    @RequestMapping("/update")
    public Boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo) {
        log.info("接收参数update");
        Integer result = null;
        try {
            result = blogService.updateBlog(blogInfo);
            if(result==1) return true;
        }catch (Exception e) {
            log.error("更新博客失败");
        }
        return false;
    }

    /**
     *  删除博客(采取逻辑删除)
     */
    @RequestMapping("/delete")
    public boolean deleteBlog(Integer blogId) {
        log.info("删除博客："+blogId);
        Assert.notNull(blogId,"blogId is null");
        Integer result = null;
        try {
            BlogInfo blogInfo = new BlogInfo();
            blogInfo.setId(blogId);
            result = blogService.deleteBlog(blogInfo);
            if(result==1) return true;
        }catch (Exception e) {
            log.error("删除博客失败");
        }
        return false;
    }

}
